/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SubtreeEditor.java
 *
 * Created on 2011-11-20, 19:14:31
 */
package pobicos;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;


/**
 *
 * @author marcin
 */
public class SubtreeEditor extends javax.swing.JFrame {

    private JungSubtreePanel graphicSubtree_; 
    private JungTreePanel graphicTree_;

    /** Creates new form SubtreeEditor */

    public SubtreeEditor(JungTreePanel graphicTree) {
        initComponents();
        graphicTree_ = graphicTree;

        Agent tempAgent = (Agent) graphicTree_.getLogicalTree().getNodeAt(graphicTree_.currentPickedVertex).getData();

        if (tempAgent.getOperatorsList() != null) {
            for (Operator o : tempAgent.getOperatorsList()) {
                OperatorsComboBox.addItem(o.getName() + ":" + o.getNumberOfInputs() + ":" + o.getNumberOfOutputs());
            }
        }

        graphicSubtree_ = new JungSubtreePanel(graphicTree_);
        
        graphicSubtree_.setUpTheTree(MainTreePanel.getPreferredSize().width, MainTreePanel.getPreferredSize().height-50);
        
        MainTreePanel.setLayout(new BorderLayout());
        
        MainTreePanel.add(graphicSubtree_.getTreeJPanel(), BorderLayout.CENTER);

        this.addComponentListener(new ComponentListener() {
            
            public void componentResized(ComponentEvent e) {
                graphicSubtree_.vvSetSize(MainTreePanel.getWidth(), MainTreePanel.getHeight());
            }
            public void componentHidden(ComponentEvent e) {
            }
            public void componentMoved(ComponentEvent e) {
            }
            public void componentShown(ComponentEvent e) {
            }
        });
        
        this.setLocationRelativeTo(graphicTree_.getMainViewFrame());
    }
    public List<Operator> getOperatorsList() {
        Agent tempAgent = (Agent) graphicTree_.getLogicalTree().getNodeAt(graphicTree_.currentPickedVertex).getData();
        return tempAgent.getOperatorsList();
    }
    public void setNewOperatorsList() {
         Agent tempAgent = (Agent) graphicTree_.getLogicalTree().getNodeAt(graphicTree_.currentPickedVertex).getData();
         tempAgent.setOperatorsList(new ArrayList<Operator>());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainTreePanel = new javax.swing.JPanel();
        NewOperatorEditorButton = new javax.swing.JButton();
        AvalOpetatorsLabel = new javax.swing.JLabel();
        OperatorsComboBox = new javax.swing.JComboBox();
        useOperatorButton = new javax.swing.JButton();
        saveStateButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        MainTreePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        MainTreePanel.setName("MainTreePanel"); // NOI18N
        MainTreePanel.setPreferredSize(new java.awt.Dimension(669, 330));

        javax.swing.GroupLayout MainTreePanelLayout = new javax.swing.GroupLayout(MainTreePanel);
        MainTreePanel.setLayout(MainTreePanelLayout);
        MainTreePanelLayout.setHorizontalGroup(
            MainTreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 749, Short.MAX_VALUE)
        );
        MainTreePanelLayout.setVerticalGroup(
            MainTreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 428, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pobicos.AdaptSysApp.class).getContext().getResourceMap(SubtreeEditor.class);
        NewOperatorEditorButton.setText(resourceMap.getString("NewOperatorEditorButton.text")); // NOI18N
        NewOperatorEditorButton.setName("NewOperatorEditorButton"); // NOI18N
        NewOperatorEditorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NewOperatorEditorButtonActionPerformed(evt);
            }
        });

        AvalOpetatorsLabel.setFont(resourceMap.getFont("AvalOpetatorsLabel.font")); // NOI18N
        AvalOpetatorsLabel.setText(resourceMap.getString("AvalOpetatorsLabel.text")); // NOI18N
        AvalOpetatorsLabel.setName("AvalOpetatorsLabel"); // NOI18N

        OperatorsComboBox.setName("OperatorsComboBox"); // NOI18N

        useOperatorButton.setText(resourceMap.getString("useOperatorButton.text")); // NOI18N
        useOperatorButton.setName("useOperatorButton"); // NOI18N
        useOperatorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                useOperatorButtonActionPerformed(evt);
            }
        });

        saveStateButton.setText(resourceMap.getString("saveStateButton.text")); // NOI18N
        saveStateButton.setName("saveStateButton"); // NOI18N
        saveStateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveStateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(MainTreePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(NewOperatorEditorButton)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(AvalOpetatorsLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 516, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(OperatorsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(useOperatorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 238, Short.MAX_VALUE)
                                .addComponent(saveStateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainTreePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AvalOpetatorsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NewOperatorEditorButton)
                    .addComponent(OperatorsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(useOperatorButton)
                    .addComponent(saveStateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void NewOperatorEditorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewOperatorEditorButtonActionPerformed
       new OperatorsTypeEditor(this).setVisible(true);
    }//GEN-LAST:event_NewOperatorEditorButtonActionPerformed

    private void useOperatorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_useOperatorButtonActionPerformed
        if(OperatorsComboBox.getSelectedItem()!=null) {
        JOptionPane.showMessageDialog(null, "Click on the graph to draw an operator");
        graphicSubtree_.drawOperator(getOperatorsList().get(OperatorsComboBox.getSelectedIndex()));
        }
    }//GEN-LAST:event_useOperatorButtonActionPerformed

    private void saveStateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveStateButtonActionPerformed
       // graphicTree_.getAllOperatorsList().addAll(graphicSubtree_.saveState(opetatorsList_));
       graphicSubtree_.saveState(getOperatorsList());
        dispose();
    }//GEN-LAST:event_saveStateButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AvalOpetatorsLabel;
    private javax.swing.JPanel MainTreePanel;
    private javax.swing.JButton NewOperatorEditorButton;
    public static javax.swing.JComboBox OperatorsComboBox;
    private javax.swing.JButton saveStateButton;
    private javax.swing.JButton useOperatorButton;
    // End of variables declaration//GEN-END:variables

}
